﻿using AutoMapper;
using Learn.Net8.IService;
using Learn.Net8.Service;
using Learn.Net8.Model;
using Learn.Net8.Repository;

namespace Learn.Net8.Service
{
    public class BaseService<TEntity, TVo> : IBaseService<TEntity, TVo>
        where TEntity : class, new()
        where TVo : class, new()
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<TEntity> _baseRepo;

        // 注意注入接口，而不是实现类
        public BaseService(IMapper mapper, IBaseRepository<TEntity> baseRepo)
        {
            _mapper = mapper;
            _baseRepo = baseRepo;
        }

        public async Task<List<TVo>> Query()
        {
            var entities = await _baseRepo.Query();
            var data = _mapper.Map<List<TVo>>(entities);
            return data;
        }
    }
}
